package com.weapp.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weapp.common.utils.RedisUtils;
import com.weapp.system.constant.CacheConstants;
import com.weapp.system.entity.User;
import com.weapp.system.mapper.user.UserMapper;
import com.weapp.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.List;


/**
 * @author jsy
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2022-07-01 14:05:43
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
  implements UserService {

  @Autowired
  private RedisUtils redisCache;
  /**
   * 项目启动时，初始化参数到缓存
   */
  @PostConstruct
  public void init()
  {
    loadingConfigCache();
  }
  @Override
  public User selectByOpenId(String openid) {
    return this.getBaseMapper().selectByOpenId(openid);
  }

  /**
   * 加载参数缓存数据
   * TODO 此处为了测试，故初始化缓存起始数据
   */
  public void loadingConfigCache()
  {
    List<User> list = list();
    if(!CollectionUtils.isEmpty(list)){
      for (User user : list) {
        redisCache.set(CacheConstants.LOGIN_TOKEN_KEY+user.getId(), user);
      }
    }
  }
}




